import { createApp } from 'vue' // Vue 3 的导入方式
import App from './App'

// uni-app 的 Vue 3 兼容性处理，确保 Vue 2 插件兼容
// 如果您遇到某些 uni-ui 组件在 Vue 3 项目中不工作，可以尝试取消注释下面这行
// import { initVueApp } from '@dcloudio/uni-h5-vue'

// 全局API配置 (建议将此部分移动到 util/api.js 或 services/api.js 文件中)
const api = {
  baseUrl: 'https://localhost:8080', // 已更新为您的实际后端API地址

  // 模拟API请求函数
  request(options) {
    return new Promise((resolve, reject) => {
      uni.request({
        url: this.baseUrl + options.url,
        method: options.method || 'GET',
        data: options.data || {},
        header: {
          'Content-Type': 'application/json',
          // 假设token存储在localStorage中
          'Authorization': uni.getStorageSync('token') ? `Bearer ${uni.getStorageSync('token')}` : ''
        },
        success: (res) => {
          if (res.statusCode >= 200 && res.statusCode < 300) {
            resolve(res.data);
          } else {
            uni.showToast({
              title: res.data.message || '请求失败',
              icon: 'none'
            });
            reject(res.data);
          }
        },
        fail: (err) => {
          uni.showToast({
            title: '网络请求失败',
            icon: 'none'
          });
          reject(err);
        }
      });
    });
  },

  // 用户相关
  login(data) { return this.request({ url: '/users/login', method: 'POST', data }); },
  register(data) { return this.request({ url: '/users/register', method: 'POST', data }); },
  getUserInfo(userId) { return this.request({ url: `/users/${userId}`, method: 'GET' }); },
  updateUserInfo(userId, data) { return this.request({ url: `/users/${userId}`, method: 'PUT', data }); },

  // 窗口相关
  getWindows(params) { return this.request({ url: '/windows', method: 'GET', data: params }); },
  getWindowDetail(windowId) { return this.request({ url: `/windows/${windowId}`, method: 'GET' }); },
  merchantUpdateWindow(windowId, data) { return this.request({ url: `/merchant/windows/${windowId}`, method: 'PUT', data }); },

  // 菜品相关
  getDishDetail(dishId) { return this.request({ url: `/dishes/${dishId}`, method: 'GET' }); },
  merchantAddDish(windowId, data) { return this.request({ url: `/merchant/windows/${windowId}/dishes`, method: 'POST', data }); },
  merchantUpdateDish(dishId, data) { return this.request({ url: `/merchant/dishes/${dishId}`, method: 'PUT', data }); },
  merchantDeleteDish(dishId) { return this.request({ url: `/merchant/dishes/${dishId}`, method: 'DELETE' }); },

  // 评价相关
  submitComment(data) { return this.request({ url: '/comments', method: 'POST', data }); },
  likeComment(commentId) { return this.request({ url: `/comments/${commentId}/like`, method: 'POST' }); },
  merchantReplyComment(commentId, data) { return this.request({ url: `/merchant/comments/${commentId}/reply`, method: 'POST', data }); },

  // 举报相关
  submitReport(data) { return this.request({ url: '/reports', method: 'POST', data }); },
  adminGetReports(params) { return this.request({ url: '/admin/reports', method: 'GET', data: params }); },
  adminProcessReport(reportId, data) { return this.request({ url: `/admin/reports/${reportId}/process`, method: 'PUT', data }); },

  // 收藏相关
  addFavorite(data) { return this.request({ url: '/favorites', method: 'POST', data }); },
  removeFavorite(favoriteId) { return this.request({ url: `/favorites/${favoriteId}`, method: 'DELETE' }); },
  getFavorites(params) { return this.request({ url: '/favorites', method: 'GET', data: params }); },

  // 公告/通知相关
  getNotifications() { return this.request({ url: '/notifications', method: 'GET' }); },
  adminPublishNotice(data) { return this.request({ url: '/admin/notices', method: 'POST', data }); },

  // 管理员/统计相关 (如果前端需要展示这些数据)
  adminGetStatistics(params) { return this.request({ url: '/admin/statistics', method: 'GET', data: params }); },
  merchantGetStatistics() { return this.request({ url: '/merchant/statistics', method: 'GET' }); },
  adminAuditContent(contentType, contentId, data) { return this.request({ url: `/admin/audit/${contentType}/${contentId}`, method: 'PUT', data }); }
};

// 创建 Vue 3 应用实例
const app = createApp(App)

// 将 API 挂载到全局属性
app.config.globalProperties.$api = api

// 挂载应用
app.mount('#app')

// 如果您需要兼容 Vue 2 的插件，可以在这里调用 initVueApp
// initVueApp(app)
